﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace StartDance
{
    public class StepManager
    {
        public List<Step> stepList;
        int MAX_STEP = 6;
        public volatile bool isAlive = true;
        public const int SLEEP_PERIOD = 1000;
        Delegate updateScore;

        public StepManager(Delegate _updateScore)
        {
            stepList = new List<Step>();
            for (int i = 0; i < MAX_STEP; i++) stepList.Add(new Step(Direction.None));
            updateScore = _updateScore;
        }

        public void generateStep()
        {
            while (isAlive)
            {                      
                int next = new Random().Next(4);
                switch (next)
                {
                    case 0: stepList.Add(new Step(Direction.Left)); break;
                    case 1: stepList.Add(new Step(Direction.Down)); break;
                    case 2: stepList.Add(new Step(Direction.Up)); break;
                    case 3: stepList.Add(new Step(Direction.Right)); break;
                }

                updateScore.DynamicInvoke(stepList[0].dir);
                if (stepList.Count > MAX_STEP) stepList.RemoveAt(0);
                Thread.Sleep(SLEEP_PERIOD);
            }
            Console.WriteLine("Stop generating step");
        }

    }

    public class Step
    {
        public Direction dir;
        public Step(Direction _dir)
        {
            dir = _dir;
        }
    }

    public enum Direction { Left, Right, Up, Down ,LeftUp,LeftDown,RightUp,RightDown,Center,None};
}
